package com.kevin.swing;

import java.awt.Component;

import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellEditor;

/**
 * 复选框
 * 
 * @ClassName CheckBoxCellEditor
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2017年8月22日 下午2:51:11
 *
 */
public class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {
    // ~ Static fields/initializers -------------------------------------------------------------------------------------

    private static final long serialVersionUID = 1L;

    // ~ Instance fields ------------------------------------------------------------------------------------------------

    protected JCheckBox checkBox;

    // ~ Constructors ---------------------------------------------------------------------------------------------------

    public CheckBoxCellEditor() {
        checkBox = new JCheckBox();
        checkBox.setHorizontalAlignment(SwingConstants.CENTER);
        // checkBox.setBackground( Color.white);
    }

    // ~ Methods --------------------------------------------------------------------------------------------------------

    @Override
    public Object getCellEditorValue() {
        return Boolean.valueOf(checkBox.isSelected());
    }

    // ~ ----------------------------------------------------------------------------------------------------------------

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        checkBox.setSelected(((Boolean) value).booleanValue());

        return checkBox;

    }

} // end class CheckBoxCellEditor
